package com.huang.springboot.controller;

import com.huang.springboot.pojo.Department;
import com.huang.springboot.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Collection;

/**
 * @author yili_gi
 */
@Controller
public class DepartmentController {

    @Autowired
    private DepartmentService departmentServiceImpl;

    @ApiOperation("查询全部部门接口")
    @GetMapping("/dep/list")
    public String toDepartmentList(Model model){
        Collection<Department> departments = departmentServiceImpl.getDepartments();
        model.addAttribute("deps",departments);
        return "/dep/list";
    }


    @GetMapping("/dep/add")
    public String toAdd(Department department){
        return "/dep/add";
    }

    @ApiOperation("添加部门接口")
    @PostMapping("/dep/add")
    public String addDepartment(Department department){
        departmentServiceImpl.addDepartment(department);
        return "redirect:/dep/list";
    }

    @GetMapping("/dep/update/{departmentId}")
    public String toUpdate(@PathVariable("departmentId") int departmentId,Model model){
        Department department = departmentServiceImpl.getDepartmentById(departmentId);
        model.addAttribute("dep",department);
        return "/dep/update";
    }

    @ApiOperation("修改部门信息接口")
    @PostMapping("/dep/update")
    public String update(Department department){
        departmentServiceImpl.updateDepartment(department);
        return "redirect:/dep/list";
    }

    @ApiOperation("删除部门接口")
    @GetMapping("/dep/remove/{departmentId}")
    public String remove(@PathVariable("departmentId") int departmentId){
        departmentServiceImpl.removeDepartment(departmentId);
        return "redirect:/dep/list";
    }

}
